package com.example.com.skills_utf8;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.RectF;
import android.os.Handler;
import android.os.Parcelable;
import android.support.annotation.NonNull;
import android.util.AttributeSet;
import android.util.Log;
import android.widget.ProgressBar;

/**
 * Created by matengfei on 15/8/10.
 */
public class _64CircleRotaProgress extends ProgressBar{


    private final String TAG = _64CircleRotaProgress.class.getSimpleName();


    Paint paint = new Paint();
    Paint paint_arc = new Paint();
    Paint paint_bitmap = new Paint();

    int radius = 200;
    int stroke_width = 25;
    RectF circle_rect = new RectF();

    //Timer timer = new Timer();
    Handler handler = new Handler();
    int progress = 0;
    int StartAngle = 0;


    Bitmap bitmap_pause;
    Bitmap bitmap_start;
    Bitmap bitmap_background;


    int status = States.pause;
    boolean bComplete = false;
    boolean bLoop = false;

    Listener listener;


    // 自定义图案
    BitmapShader bitmapshader_diy;
    Paint paint_diy ;



    boolean bFirstClick = false;
    boolean bClickPure = false;


    Matrix matrix = new Matrix();





    public _64CircleRotaProgress(Context context, AttributeSet attrs) {
        super(context, attrs);


        paint.setColor(Color.BLACK);
        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeWidth(stroke_width);
        paint.setAntiAlias(true);

        paint_arc.setColor(Color.YELLOW);
        paint_arc.setStyle(Paint.Style.STROKE);
        paint_arc.setStrokeWidth(stroke_width);
        paint_arc.setAntiAlias(true);



        handler.postDelayed(new MyRunnable(), 1000);



        // 加载暂停，播放按钮
        //BitmapDrawable bitmapdrawable_pause = (BitmapDrawable)getResources().getDrawable(R.drawable.pause_circle, context.getTheme());
        //bitmap_pause = bitmapdrawable_pause.getBitmap();

        bitmap_pause = BitmapFactory.decodeResource(getResources(), R.drawable.pause_circle);
        bitmap_start = BitmapFactory.decodeResource(getResources(), R.drawable.start_circle);

        //bitmap_background = BitmapFactory.decodeResource(getResources(), R.drawable.circle_background);


        // 使用图案填充
        //bitmapshader_diy = new BitmapShader(bitmap_pause, Shader.TileMode.REPEAT, Shader.TileMode.REPEAT);
        //paint_diy.setShader(bitmapshader_diy);


        // 背景呈现不规则向外扩散，使用粒子效果的原理即可，使用灰度图与颜色进行alpha混合；
        // 在播放上有个淡入淡出效果




    }


    // 内存不足时保存成员变量
    @NonNull
    @Override
    public Parcelable onSaveInstanceState() {
        return super.onSaveInstanceState();
    }

    @Override
    public void onRestoreInstanceState(Parcelable state) {
        super.onRestoreInstanceState(state);
    }


    float sx = 1;
    float sy = 1;
    //int delta = 5;
    float delta = -0.005f;

    /*
    arc旋转角度问题
	-90
180		0
	90

    */
    @Override
    protected synchronized void onDraw(Canvas canvas) {
       // super.onDraw(canvas);


        StartAngle +=10;
        StartAngle %= 360;


        sx +=delta;
        sy +=delta;

        // 控制缩放
        if(sx<0.01f){
            delta = Math.abs(delta);
        }
        else if(sx>1){
            delta = -Math.abs(delta);
        }

        if(sy<0.01f){
            delta = Math.abs(delta);
        }
        else if(sy>1){
            delta = -Math.abs(delta);
        }


        Log.d(TAG, "sx: "+sx+" sy: "+sy);

        //canvas.save();

        // 或者缩放整个矩形
        circle_rect.set(getWidth() / 2 - radius, getHeight() / 2 - radius, getWidth() / 2 + radius, getHeight() / 2 + radius);


        canvas.scale(sx, sy);

        canvas.drawArc(circle_rect, StartAngle, 90, false, paint_arc);
        canvas.drawArc(circle_rect, StartAngle + 180, 90, false, paint_arc);

        // 如果在这里 canvas.scale()就影响不到上面的绘制圆弧；

        //canvas.restore();
        canvas.drawRect(circle_rect, paint);




    }

    public interface Listener{
        public void OnComplete();
    }




    public void click(){
        if(status == States.pause){
            status = States.start;

            if(!bFirstClick){
                bFirstClick = true;
                bClickPure = true;
            }

        }
        else{
            status = States.pause;
        }


        invalidate();
    }



    class States{
        public static final int pause = 0;
        public static final int start = 1;
        public static final int stop = 2;

    }



    class MyRunnable implements Runnable {

        @Override
        public void run() {


            invalidate();
            handler.postDelayed(new MyRunnable(), 100);

        }
    }



}
